<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:24}" :md="{span:8}" :xl="{span:6}">
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col ::sm="{span:24}" :md="{span:8}" :xl="{span:6}">
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zszym" @change="zyChange">
                <a-select-option v-for="(i,j) in selectMenu.zy" :key="j" :value="i.zyh">{{ i.zyh+'-'+i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col ::sm="{span:24}" :md="{span:8}" :xl="{span:6}">
              <a-form-item label="招生年度">
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsnj" @click='zsndChange'
                  disabled>
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :sm="{span:16}" :xl="{span:8}" style="padding-top: 29px;">
            <a-button type="primary" @click="handleReport" :disabled="!upStatus">上报</a-button>
            <a-button type="primary" @click="handleCreate">添加</a-button>
            <a-button type="primary" @click="loadData">查询</a-button>
            <a-button type="primary" @click="exportExcel">导出</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
              {{ advanced ? '收起' : '展开' }}
              <a-icon :type="advanced ? 'up' : 'down'" />
            </a>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table ref="table" size="default" rowKey="key" :columns="columns" :loading="loading" :dataSource="data" :alert="options.alert"
      :rowSelection="options.rowSelection" :pagination="pagination" @change="handleTableChange">
      <template slot="action" slot-scope="text,record">
        <a v-if="record.id" @click="record.sbzt!=1 && handleEdit(record.id)" :style="record.sbzt!='1'?'':'color:#999'">修改</a>
		<a @click="record.sbzt!=1 &&handleDelete(record.id)" :style="record.sbzt!='1'?'':'color:#999'">删除</a>
      </template>
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table>
    <!-- 新增 -->
    <professional-add ref="professionalAdd" @close="handleCreateClose" @success="handleCreateSuccess"
      :professionalAddVisiable="professionalAddVisiable"></professional-add>
    <!-- 编辑 -->
    <professional-edit ref="professionalEdit" @close="handleEditClose" @success="handleEditSuccess" :info="professionalEditData"
      :professionalEditVisiable="professionalEditVisiable"></professional-edit>
  </a-card>
</template>

<script>
  // import moment from 'moment'
  import ProfessionalAdd from './professionalAdd'
  import ProfessionalEdit from './professionalEdit'
  import {
    STable
  } from '~'
  import {
    mapState
  } from 'vuex'

  const selectArr = ['TYPE0311', 'TYPE0312', 'TYPE0126', 'TYPE0075', 'TYPE0056', 'TYPE0083', 'TYPE0125', 'TYPE0072']
  export default {
    name: 'Professional',
    components: {
      STable,
      ProfessionalAdd,
      ProfessionalEdit
    },
    data() {
      return {
        advanced: true,
        loading: false,
        data: [],
        upStatus:true,
        // 查询参数
        queryParam: {
          zsnj: '',
          zsyxm: '',
          zszym: ''
        },
        postParams: {},
        // 表头
        columns: [{
          title: '院系编码',
          dataIndex: 'zsyxm'
        },
          {
            title: '院系',
            dataIndex: 'dwmc'
          },
          {
            title: '专业代码',
            dataIndex: 'zszym'
          },
          {
            title: '专业名称',
            dataIndex: 'zymc'
          },
          {
            title: '总计划',
            dataIndex: 'zszjhrs'
          },
          {
            title: '培养模式',
            dataIndex: 'pymsmc'
          },
          {
            title: '备注',
            dataIndex: 'note',
            width:320
          },
          {
            title: '操作',
            dataIndex: 'cz',
            fixed: 'right',
            width: 120,
            scopedSlots: {
              customRender: 'action'
            }
          }
        ],

        // custom table alert & rowSelection
        options: {
          alert: {
            show: false
          }
        },
        visible: false,
        confirmLoading: false,
        editId: null,
        selectMenu: {
          yx: [], // 院系
          zy: [] // 所属专业
        },
        professionalAddVisiable: false,
        professionalEditVisiable: false,
        professionalEditData: {
          hzfs: '',
          zjh: '',
          fzyxwjh: '',
          zyxwjh: ''
        },
        pagination: {
          current: 1
        },
        isInclodeTime: false
      }
    },
    computed: {
      ...mapState({
        systemInfo: state => state.app.systemInfo,
        TYPE0126: state => state.app['TYPE0126'] // 年级
      })
    },
    created() {

      this.initSelectMenu()
      selectArr.forEach(v => {
        this.$store.dispatch('app/setDictionary', v)
      })
    },
    methods: {
      initSelectMenu() {
        let allRequest = [
          this.$api.base.collegeAll()
        ]
        Promise.all(allRequest).then(res => {
          this.selectMenu = {
            yx: res[0].data,
            zy: []
          }
          this.queryParam.zsyxm = res[0].data[0].dwh;
          this.$api.base.majorAll({
            lsdwh: this.queryParam.zsyxm
          }).then(res => {
            this.$set(this.selectMenu, 'zy', res.data)
          })

          this.$api.base.getSchoolYear().then(res => {
            this.queryParam.zsnj = res.data.doctoralAdmissionYear;
            this.$api.doctorRecruit.professionalAble({}).then(res => {
              if (res.data && res.data.addable) {
                this.isInclodeTime = true
              } else {
                this.isInclodeTime = false
              }
              this.loadData({
                pageNum: 1,
                pageSize: 10
              })
            })

          })
        })

      },
      // 选择院系
      yxChange(code) {
        console.log(code)
        this.$set(this.selectMenu, 'zy', '')
        if (code) {
          this.postParams.zsyxm = code

          // var yx = this.selectMenu.yx
          // var xxlx = this.selectMenu.xxlx
          this.queryParam.zszym = ''
          this.$api.base.majorAll({
            lsdwh: code
          }).then(res => {
            if (res.data) {
              this.selectMenu.zy = res.data
            }
          })
        }
      },
      // 选择招生年度
      zsndChange(code) {
        this.postParams.zsnj = code
      },
      // 选择专业
      zyChange(code) {
        this.postParams.zsyxm = code
      },
      // 导出
      exportExcel() {
        this.$export('admissions/bszsjh/export', this.queryParam)
      },
      // 加载列表

      loadData(parameter = {}) {
        if (!parameter.pageNum) {
          this.pagination.current = 1
          parameter.pageNum = 1
        }
        if (this.isInclodeTime) {
          let params = Object.assign(this.queryParam, parameter)
          this.loading = true;
          this.$api.doctorRecruit.professionalList(params).then(res => {
            // console.log(res)
            const pagination = { ...this.pagination
            };
            pagination.total = 0
            if (res.data && res.data.total) {
              pagination.total = res.data.total;
            }
            this.data = []
            this.pagination = pagination;
            if (res.data && res.data.rows.length) {
              this.data = res.data.rows;
            }
            this.loading = false;
          })
        } else {
          this.$message.warning('当前时间不在分配专业招生计划时间范围内!')
          return false
        }
        this.buttonAble({
          zsnj :this.queryParam.zsnj,
          zsyxm:this.queryParam.zsyxm
        })
      },
      // 分页
      handleTableChange(pagination, filters, sorter) {
        console.log(pagination);
        const pager = { ...this.pagination
        };
        pager.current = pagination.current;
        this.pagination = pager;
        this.loadData({
          pageSize: 10,
          pageNum: pagination.current
        });
      },
      // 添加
      handleCreate() {
        if (this.isInclodeTime) {
          this.professionalAddVisiable = true
        } else {
          this.$message.warning('当前时间不在分配专业招生计划时间范围内!')
          return false
        }
      },
      // 关闭添加弹层
      handleCreateClose() {
        this.professionalAddVisiable = false
      },
      // 添加成功
      handleCreateSuccess() {
        this.professionalAddVisiable = false
        this.$message.success('添加成功')
        this.loadData({
          pageNum: this.pagination.current,
          pageSize: 10
        })
      },
      // 修改
      handleEdit(key) {
        console.log()
        this.$api.doctorRecruit.professionalId(key).then(res => {
          this.departmentsEditVisiable = true
          if (res.data) {
            this.professionalEditData = res.data
          }
        })
        this.professionalEditVisiable = true
      },
      // 关闭添加弹层
      handleEditClose() {
        this.professionalEditVisiable = false
      },
	    // 删除
    handleDelete (key) {
      let _this=this;
      this.$confirm({
         title: '确定删除?',
         onOk() {
          _this.$api.doctorRecruit.professionalDel(key).then(res=>{
        if(res.status==1){
          _this.$message.success(res.msg)
          _this.loadData({
            pageSize:10,
            pageNum:_this.pagination.current
          })
        }else{
          _this.$message.warning(res.msg)
        }
      })
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });

    },
      // 添加成功
      handleEditSuccess() {
        this.professionalEditVisiable = false
        this.$message.success('添加成功')
        this.loadData({
          pageNum: this.pagination.current,
          pageSize: 10
        })
      },
      // 上报
      handleReport () {
        var that = this;
        this.$confirm({
          title: '确定上报?',
          onOk() {
            console.log('OK');
            let query ={
              zsnj : that.queryParam.zsnj,
              zsyxm : that.queryParam.zsyxm
            }
            that.$api.doctorRecruit.professionalUp(query).then(res => {

              that.loadData({
                pageSize:10,
                pageNum:that.pagination.current
              })

            })
          },
          onCancel() {

          },
          class: 'test',
        });
      },
      //判断上报状态是否置灰
      buttonAble(query){
        this.$api.doctorRecruit.professionalCan(query).then(res => {
          this.upStatus = res.data
        })
      }
    }
  }
</script>
